package TCP.TCP7;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TcpServer {
    private ServerSocket listenSocket;
    public TcpServer(int port) throws IOException {
        this.listenSocket = new ServerSocket(port);
    }
    public void start() throws IOException {
        System.out.println("服务器启动");
        //用线程池来处理多个客户
        ExecutorService pool = Executors.newCachedThreadPool();
        while(true) {
            //等待与客户端建立连接
            Socket clientSocket = listenSocket.accept();
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        processClient(clientSocket);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    private void processClient(Socket clientSocket) throws IOException {
        System.out.printf("[%s:%d] 客户端上线", clientSocket.getInetAddress(), clientSocket.getPort());
        try(InputStream inputStream = clientSocket.getInputStream();
        OutputStream outputStream = clientSocket.getOutputStream()) {
            Scanner in = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            while(true) {
                //1.获取客户端请求
                if(!in.hasNext()) {
                    System.out.printf("[%s:%d] 客户端下线", clientSocket.getInetAddress(), clientSocket.getPort());
                    break;
                }
                String request = in.next();
                //2.根据请求计算响应
                String response = process(request);
                //3.将响应发送给客户端
                printWriter.println(response);
                printWriter.flush();
                //4.打印日志
                System.out.printf("[%s:%d] req: %s, resp: %s\n", clientSocket.getInetAddress().toString(),
                        clientSocket.getPort(), request, response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            clientSocket.close();
        }
    }

    private String process(String request) {
        return request;
    }

    public static void main(String[] args) throws IOException {
        TcpServer tcpServer = new TcpServer(8080);
        tcpServer.start();
    }
}







class demo {
    private ServerSocket listenSocket;
    //绑定端口号
    public demo(int port) throws IOException {
        listenSocket = new ServerSocket(port);
    }
    //启动服务器
    public void start() throws IOException {
        System.out.println("服务器启动~");
        //使用线程池来处理多各客户端情况，避免频繁创建销毁线程
        ExecutorService pool = Executors.newCachedThreadPool();
        while(true) {
            //等待建立连接
            Socket clientSocket = listenSocket.accept();
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        processConnetion(clientSocket);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    private void processConnetion(Socket clientSocket) throws IOException {
        System.out.printf("[%s:%d] 客户端上线~", clientSocket.getInetAddress(),
                clientSocket.getPort());
        try(InputStream inputStream = clientSocket.getInputStream();
            OutputStream outputStream = clientSocket.getOutputStream()) {
            while(true) {
                //1.接收请求
                Scanner in = new Scanner(inputStream);
                if(!in.hasNext()) {
                    System.out.printf("[%s:%d] 客户端下线~", clientSocket.getInetAddress(),
                            clientSocket.getPort());
                    break;
                }
                String request = in.next();
                //2.根据请求计算响应
                String response = process(request);
                //3.将响应返回给客户端
                PrintWriter printWriter = new PrintWriter(outputStream);
                printWriter.println(response);
                //刷新缓冲区
                printWriter.flush();
                //4.打印日志
                System.out.printf("[%s:%d] req: %s, resp: %s", clientSocket.getInetAddress().toString(),
                        clientSocket.getPort(), request, response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            clientSocket.close();
        }
    }

    private String process(String request) {
        return request;
    }

    public static void main(String[] args) throws IOException {
        demo tcpServer = new demo(8080);
        tcpServer.start();
    }
}
